
    
var tuiwa = require("../../tuiwa.js");

Page({
  data: {
    testLoadingLayer: true,
    widthProgress: 0,
    title: "",
    desc: "",
    multiple: "",
    titleDescBg: "",
    testBtnBg: "",
    testResultBg: "",
    tilu: [],
    testResultList: []
  },
  onLoad: function (options) {
    if(!tt.getStorageSync('openId')){
      tuiwa.bindDyWx()
    }
    
    setTimeout(() => {
      this.setData({
        testLoadingLayer: false
      })
    }, 4000)
    this.setData({
      title: options.title,
      desc: options.desc,
      multiple: options.multiple,
      titleDescBg: options.titleDescBg,
      testBtnBg: options.testBtnBg,
      testResultBg: options.testResultBg,
      timu: JSON.parse(options.timu),
      testResultList: JSON.parse(options.testResultList)
    })
  },
  backClick(){
    tt.navigateBack();
  },
  getRandomArrayElements(arr, count) { // 随机选择几道题目
    var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
    while (i-- > min) {
      index = Math.floor((i + 1) * Math.random());
      temp = shuffled[index];
      shuffled[index] = shuffled[i];
      shuffled[i] = temp;
    }
    return shuffled.slice(min);
  },
  startTestClick() {
    var title = this.data.title
    if (title == "2021和你有缘分的字") {
      var timu = JSON.stringify(this.getRandomArrayElements((this.data.timu), 6))
    } else if (title == "2021春晚测试") {
      var timu = JSON.stringify(this.getRandomArrayElements((this.data.timu), 7))
    } else if (title == "智商测试题") {
      var timu1 = []
      var timu2 = []
      var timu3 = []
      var timu4 = []
      var timu5 = []
      this.data.timu.forEach((item, index) => {
        if (index <= 7) {
          timu1.push(item)
        } else if (index >= 8 && index <= 14) {
          timu2.push(item)
        } else if (index >= 15 && index <= 24) {
          timu3.push(item)
        } else if (index >= 25 && index <= 28) {
          timu4.push(item)
        } else {
          timu5.push(item)
        }
      })
      var timuRes1 = this.getRandomArrayElements(timu1, 2)
      var timuRes2 = this.getRandomArrayElements(timu2, 2)
      var timuRes3 = this.getRandomArrayElements(timu3, 4)
      var timuRes4 = this.getRandomArrayElements(timu4, 1)
      var timuRes5 = this.getRandomArrayElements(timu5, 1)
      var timu = JSON.stringify(timuRes1.concat(timuRes2).concat(timuRes3).concat(timuRes4).concat(timuRes5))
    } else {
      var timu = JSON.stringify(this.data.timu)
    }
    var multiple = this.data.multiple
    var testResultBg = this.data.testResultBg
    var testResultList = JSON.stringify(this.data.testResultList)
    var titleDescBg = this.data.titleDescBg
    var testBtnBg = this.data.testBtnBg
    var dataInfo = {
      title,multiple,testResultBg,testResultList,timu,titleDescBg,testBtnBg
    }
    tt.setStorageSync('dataInfo', dataInfo);
    tt.navigateTo({
      url: `/pages/testing/testing?multiple=${multiple}&title=${title}&timu=${timu}&testResultList=${testResultList}&testResultBg=${testResultBg}`
    });

    if(title == "手机号码运势测评") {
      tt.navigateTo({
        url:`/pages/phoneTest/phoneTest`
      })
    }
  },
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function (options) {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})